// 简易缓存,创建一个实现Fn(u32) -> u32的特征闭包的结构体
struct Cacher<T>
    where 
    T: Fn(u32) -> u32 
    //^试-试把Fn替换成FnMut,FnOnce有没有差别
    {
    query: T,
    value: Option<u32>,
}
impl<T> Cacher<T> 
    where
        T: Fn(u32) -> u32 {
    //^试-试把Fn替换成FnMut,FnOnce有没有差别
            fn new(query: T) -> Cacher<T>{Self {
              query,
              value: None,
            }}
// 先查询缓存值`self.value` 若不存在,则调用`query`加载
            fn value(&mut self, arg: u32) -> u32 {
                match self.value {
                    Some(v) => v,
                    None => {
                        let v = (self.query)(arg);
                        self.value = Some(v);
                        v
                    }
                }
            }
        }
fn main(){
    let mut cacher = Cacher::new(|x|x);
    let value = 2;
    println!("First,value of cacher is {}",cacher.value(value));
    let value = 3;
    //到此value值不再为空，cacher的值还是2
    println!("Second,value of cacher is {}",cacher.value(value));

}
